#!/bin/bash

# 修复剩余的 selectList 分页问题

echo "开始修复剩余的 selectList 分页问题..."
echo "=========================================="

# 定义需要修复的文件列表（除了已经修复的 CouponGroupServiceImpl.java）
files=(
    "fuint-application/src/main/java/com/fuint/common/service/impl/ArticleServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/StockServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/CateServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/UserActionServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/SendSmsServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/BookCateServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/MemberGroupServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/BannerServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/DutyServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/StoreServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/UserGradeServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/SettlementServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/ConfirmLogServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/SmsTemplateServiceImpl.java"
    "fuint-application/src/main/java/com/fuint/common/service/impl/BookServiceImpl.java"
)

# 备份文件
echo "创建备份..."
for file in "${files[@]}"; do
    if [ -f "$file" ]; then
        cp "$file" "$file.backup.$(date +%Y%m%d_%H%M%S)"
        echo "已备份: $file"
    fi
done

echo ""
echo "开始修复文件..."

# 修复每个文件
for file in "${files[@]}"; do
    if [ -f "$file" ]; then
        echo "正在修复: $file"
        
        # 使用 sed 进行替换
        # 1. 替换 selectList 为 selectPage 的模式
        sed -i.tmp '
        # 在分页方法中查找 selectList 模式并替换
        /PaginationRequest.*paginationRequest/,/return.*paginationResponse/ {
            # 替换 List<Entity> dataList = mapper.selectList(lambdaQueryWrapper);
            s/List<\([^>]*\)> dataList = \([a-zA-Z0-9_]*\)\.selectList(lambdaQueryWrapper);/Page<\1> page = new Page<>(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());\
        Page<\1> pageResult = \2.selectPage(page, lambdaQueryWrapper);\
        List<\1> dataList = pageResult.getRecords();/
            
            # 删除错误的 PageRequest 和 PageImpl 行
            /PageRequest pageRequest = PageRequest\.of/d
            /PageImpl pageImpl = new PageImpl/d
            
            # 修复 PaginationResponse 构造
            s/new PaginationResponse(pageImpl,/new PaginationResponse(pageResult,/
            s/paginationResponse\.setTotalPages(pageResult\.getPages());/\/\/ Total pages handled by PaginationHelper/
            s/paginationResponse\.setTotalElements(pageResult\.getTotal());/\/\/ Total elements handled by PaginationHelper/
            s/paginationResponse\.setContent(dataList);/\/\/ Content handled by PaginationHelper/
        }
        ' "$file"
        
        # 删除临时文件
        rm -f "$file.tmp"
        
        echo "  ✅ 修复完成"
    else
        echo "  ❌ 文件不存在: $file"
    fi
done

echo ""
echo "修复完成！"
echo "=========================================="
echo "请运行以下命令验证修复结果："
echo "bash check_selectlist_in_pagination.sh"
